﻿using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using Avalonia.Threading;
using MilkTeaMachine.OfflineClient.ViewModels;
using ReactiveUI;

namespace MilkTeaMachine.OfflineClient.Views;

public partial class AutoRunningScanOrderUserControl : ReactiveUserControl<AutoRunningScanOrderViewModel>
{
    public AutoRunningScanOrderUserControl()
    {
        AvaloniaXamlLoader.Load(this);
        this.WhenActivated(_ => { });
    }

    private void InputElement_OnLostFocus(object? sender, RoutedEventArgs e)
    {
        var textBox = (TextBox)sender!;
        Dispatcher.UIThread.InvokeAsync(() =>
        {
            textBox.Text = string.Empty;
        });
        this.FindControl<TextBox>("FocusTextBox")?.Focus();
    }

    private void Control_OnLoaded(object? sender, RoutedEventArgs e)
    {
        this.FindControl<TextBox>("FocusTextBox")?.Focus();
    }
}